package cn.gp.chatclient.ui.frame;

import cn.gp.chatclient.ClientMain;
import cn.gp.chatclient.model.UserVo;
import cn.gp.chatclient.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;

/**
 * @ClassName RegisterDialog
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 10:14
 * @Version JDK17
 */
public class RegisterDialog extends JDialog {
    private final UserService userService;
    private final Runnable onRegisterSuccess;

    public RegisterDialog(UserService userService, Runnable onRegisterSuccess) {
        this.userService = userService;
        this.onRegisterSuccess = onRegisterSuccess;
        initUI();
    }
    private void initUI() {
        setTitle("用户注册");
        setSize(350, 300);
        setLocationRelativeTo(null); // 窗口居中显示
        setModal(true); // 设置为模态对话框
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 用户名标签和输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(new JLabel("用户名:"), gbc);

        JTextField usernameField = new JTextField(15);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        add(usernameField, gbc);

        // 密码标签和输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        add(new JLabel("密码:"), gbc);

        JPasswordField passwordField = new JPasswordField(15);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        add(passwordField, gbc);

        // 昵称标签和输入框
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        add(new JLabel("昵称:"), gbc);

        JTextField nicknameField = new JTextField(15);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        add(nicknameField, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        JButton registerBtn = new JButton("注册");
        JButton backBtn = new JButton("返回");
        buttonPanel.add(registerBtn);
        buttonPanel.add(backBtn);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.CENTER;
        add(buttonPanel, gbc);

        // 注册按钮事件
        registerBtn.addActionListener(e -> {
            String username = usernameField.getText().trim();
            String password = new String(passwordField.getPassword()).trim();
            String nickname = nicknameField.getText().trim();

            // 输入验证
            if (username.isEmpty() || password.isEmpty() || nickname.isEmpty()) {
                JOptionPane.showMessageDialog(this, "所有字段都不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (password.length() < 6) {
                JOptionPane.showMessageDialog(this, "密码长度不能少于6位", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 调用注册服务
            userService.register(username, password, nickname,
                    user -> {
                        dispose();
                        onRegisterSuccess.run();
                    },
                    error -> JOptionPane.showMessageDialog(this, error, "注册失败", JOptionPane.ERROR_MESSAGE)
            );
        });

        // 返回按钮事件
        backBtn.addActionListener(e -> {
            dispose();
            ClientMain.showSelectionDialog();
        });

        // 设置回车键默认触发注册按钮
        getRootPane().setDefaultButton(registerBtn);
    }

}
